package com.pond.pond.controller;

import com.pond.common.core.web.controller.BaseController;
import com.pond.common.core.web.domain.AjaxResult;
import com.pond.common.core.web.page.TableDataInfo;
import com.pond.common.log.annotation.Log;
import com.pond.common.log.enums.BusinessType;
import com.pond.pond.domain.FishReturn;
import com.pond.pond.domain.dto.FishReturnRequest;
import com.pond.pond.service.IFishReturnService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 回鱼记录管理控制器
 */
@Tag(name = "回鱼记录管理", description = "回鱼记录相关接口")
@RestController
@RequestMapping("/fishReturn")
@Validated
public class FishReturnController extends BaseController {

    @Autowired
    private IFishReturnService fishReturnService;

    /**
     * 查询订单列表（用于回鱼操作）
     */
    @Operation(summary = "查询订单列表（用于回鱼操作）")
    @GetMapping("/list")
    public TableDataInfo list(com.pond.pond.domain.dto.OrderQueryRequest query) {
        startPage();
        List<com.pond.pond.domain.vo.OrderVO> list = fishReturnService.selectOrderListForFishReturn(query);
        return getDataTable(list);
    }

    /**
     * 查询回鱼记录列表
     */
    @Operation(summary = "查询回鱼记录列表")
    @GetMapping("/records")
    public TableDataInfo getRecords(FishReturn fishReturn) {
        startPage();
        List<FishReturn> list = fishReturnService.selectFishReturnList(fishReturn);
        return getDataTable(list);
    }

    /**
     * 根据ID查询回鱼记录详情
     */
    @Operation(summary = "查询回鱼记录详情")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@Parameter(description = "回鱼记录ID") @PathVariable Long id) {
        FishReturn fishReturn = fishReturnService.selectFishReturnById(id);
        return fishReturn != null ? AjaxResult.success(fishReturn) : AjaxResult.error("回鱼记录不存在");
    }

    /**
     * 根据订单ID查询回鱼记录列表
     */
    @Operation(summary = "根据订单ID查询回鱼记录列表")
    @GetMapping("/order/{orderId}")
    public AjaxResult getByOrderId(@Parameter(description = "订单ID") @PathVariable Long orderId) {
        List<FishReturn> list = fishReturnService.selectFishReturnByOrderId(orderId);
        return AjaxResult.success(list);
    }

    /**
     * 新增回鱼记录
     */
    @Operation(summary = "新增回鱼记录")
    @Log(title = "回鱼记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody FishReturnRequest request) {
        return fishReturnService.insertFishReturn(request);
    }

    /**
     * 修改回鱼记录
     */
    @Operation(summary = "修改回鱼记录")
    @Log(title = "回鱼记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody FishReturn fishReturn) {
        return fishReturnService.updateFishReturn(fishReturn);
    }

    /**
     * 删除回鱼记录
     */
    @Operation(summary = "删除回鱼记录")
    @Log(title = "回鱼记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@Parameter(description = "回鱼记录ID") @PathVariable Long id) {
        return fishReturnService.deleteFishReturnById(id);
    }

    /**
     * 批量删除回鱼记录
     */
    @Operation(summary = "批量删除回鱼记录")
    @Log(title = "回鱼记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch/{ids}")
    public AjaxResult removeBatch(@Parameter(description = "回鱼记录ID，多个ID用逗号分隔") @PathVariable String ids) {
        return fishReturnService.deleteFishReturnByIds(ids);
    }

    /**
     * 更新回鱼记录支付状态
     */
    @Operation(summary = "更新回鱼记录支付状态")
    @Log(title = "回鱼记录", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/pay")
    public AjaxResult updatePayStatus(
            @Parameter(description = "回鱼记录ID") @PathVariable Long id,
            @Parameter(description = "支付状态") @RequestParam Integer payStatus,
            @Parameter(description = "支付方式") @RequestParam(required = false) Integer payType) {
        return fishReturnService.updatePayStatus(id, payStatus, payType);
    }

    /**
     * 统计订单回鱼总金额
     */
    @Operation(summary = "统计订单回鱼总金额")
    @GetMapping("/order/{orderId}/totalAmount")
    public AjaxResult getTotalAmountByOrderId(@Parameter(description = "订单ID") @PathVariable Long orderId) {
        java.math.BigDecimal totalAmount = fishReturnService.sumAmountByOrderId(orderId);
        return AjaxResult.success("查询成功", totalAmount);
    }
}